package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.dto.TaskDTO;
import com.example.train_operate.dto.UserDTO;
import com.example.train_operate.entity.Host;
import com.example.train_operate.service.HostService;
import com.example.train_operate.mapper.HostMapper;
import com.example.train_operate.util.Result;
import com.example.train_operate.util.UserHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.example.train_operate.util.FileUrlConstants.FILE_URL;

/**
* @author 21609
* @description 针对表【tb_host】的数据库操作Service实现
* @createDate 2025-08-13 17:06:39
*/
@Service
public class HostServiceImpl extends ServiceImpl<HostMapper, Host>
    implements HostService{

    @Resource
    private HostMapper hostMapper;
    /**
     * 添加列控主机检修任务
     * @param host
     * @return
     */
    @Override
    public Result addHost(Host host) {
        try{
            UserDTO user = UserHolder.getUser();
            Integer userId = user.getId();
            host.setUserId(userId);
            String photoUrl = host.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                host.setPhotoUrl(substring);
            }
            hostMapper.insert(host);
            QueryWrapper<Host> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId)
                    .orderByDesc("create_time")
                    .last("limit 1");
            Host host1 = hostMapper.selectOne(queryWrapper);
            return Result.ok(host1).message("列控主机检修已完成");
        }catch (Exception e){
            return Result.fail().message("出错了");
        }
    }

    /**
     * 根据id查询列控主机检修任务
     * @param hostId
     * @return
     */
    @Override
    public Result getHostById(Integer hostId) {
        try{
            QueryWrapper<Host> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("host_id", hostId)
                    .eq("is_delete", 0);
            Host host = hostMapper.selectOne(queryWrapper);
            if(host == null){
                return Result.fail().message("没有此条列控主机检修信息");
            }
            String photoUrl = host.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String url = FILE_URL + photoUrl;
                host.setPhotoUrl(url);
            }
            return Result.ok(host);
        }catch (Exception e){
            return Result.fail();
        }
    }

    /**
     * 修改列控主机检修任务
     * @param host
     * @return
     */
    @Override
    public Result updateHost(Host host) {
        try{
            String photoUrl = host.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                host.setPhotoUrl(substring);
            }
            hostMapper.updateById(host);
            Host host1 = hostMapper.selectById(host.getHostId());
            return Result.ok(host1).message("修改成功");
        }catch (Exception e){
            return Result.fail().message("修改失败");
        }
    }
}




